package cn.bdqn.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EnCodingFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//设置请求和响应编码格式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//限制用户登录
		HttpServletRequest  req = (HttpServletRequest) request;
		HttpServletResponse  resp = (HttpServletResponse) response;
		System.out.println(req.getRequestURI());
		String url = req.getRequestURI();
		//当前的url进行判断，是否是登陆页面
		/*String[] path ={"/smbms/css/style.css", "/smbms/css/page.css", "/smbms/css/public.css",
				 "/smbms/js/jquery.js", "/smbms/js/js.js", "/smbms/js/time.js",
				 "/smbms/jsp/login.jsp","/smbms/jsp/loginServlet"};*/
		/*for(int i=0; i<path.length; i++){
			if(url)
		}*/
		if(req.getSession().getAttribute("user")==null && (url.indexOf("login")!=-1 || 
				url.indexOf("css")!=-1 || url.indexOf("js/")!=-1 ) || url.indexOf("img")!=-1 ) {
			chain.doFilter(request, response);
		}else if(req.getSession().getAttribute("user")!=null){
			chain.doFilter(request, response);
		}else{
			resp.sendRedirect("jsp/login.jsp");
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
